import {
  BookMode
} from '../../modules/book_mode.js'
const bookmode = new BookMode()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    books: [],
    bookdetail: [],
    bookshortcomment: [],
    posting: false

  },

  /**
   * 生命周期函数--监听页面加载
   */


  onLoad: function(options) {
    wx.showLoading()
    const aid = options.aid
    let resarr = []
    const getbookarr = [bookmode.getHotBook(), bookmode.getHotBook2(), bookmode.getHotBook3()]

    Promise.all(getbookarr).then(res => {
      this._getMethods(res[0], resarr, 0)
      this._getMethods(res[1], resarr, 10)
      res[2].map(item => {
        item.aid = Math.abs(item.id)
      })
      this.setData({
        bookdetail: this.data.books[aid],
        bookshortcomment: res[2]
      })
      wx.hideLoading()
    })

  },

  _getMethods(res, resarr, val) {
    res.map((item, i) => {
      item.imgSrc = item.album_500_500.substr(0, item.album_500_500.length - 16)
      item.aid = i + val
      resarr.push(item)
    })
    this.setData({
      books: resarr
    })

  },

  // ······························································
  onLike(e) {
    let haveGive = e.detail.havrGive
    // likemode.like(haveGive, this.data.classData.area, this.data.classData.country)
  },




  onFakePost() {
    this.setData({
      posting: true
    })
  },
  onCancel() {
    this.setData({
      posting: false
    })
  },

  onPost(e) {
    const name = e.detail.text || e.detail.value
    if (!name.trim()) {
      wx.showToast({
        title: '内容为空，请重新输入',
        icon: 'none',
        duration: 1000
      })
    }
    this.data.bookshortcomment.unshift({
      name: name,
      aid: 1
    })
    this.setData({
      bookshortcomment: this.data.bookshortcomment,
      posting: false
    })

    if (this.data.posting == false) {
      wx.showToast({
        title: '+1',
        icon: 'none',
        duration: 1000
      })
    }

    if (name > 12) {
      wx.showToast({
        title: '短评只能12个字',
        icon: 'none',
        duration: 1500
      })
      return
    }
    // bookmode.postComment(this.data.book.aid, comment)
    //   .then(res => {
    //   })
    //   this.data.comment.unshift({   })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})